import axios from 'axios';

// 获取 token 的函数
const getToken = () => {
  const user = sessionStorage.getItem('user');
  return user ? JSON.parse(user).passWord : '';
};

// 封装 get 请求
export const getRequest = async (url) => {
  try {
    const response = await axios.get(url, {
      headers: {
        'Content-Type': 'application/json',
        token: getToken(),
      },
    });
    return response.data;
  } catch (error) {
    console.error('请求出错:', error);
    throw error;
  }
};

// 封装 post 请求
export const postRequest = async (url, data) => {
  try {
    const response = await axios.post(url, data, {
      headers: {
        'Content-Type': 'application/json',
        token: getToken(),
      },
    });
    return response.data;
  } catch (error) {
    console.error('请求出错:', error);
    throw error;
  }
};